// BadgeWallActivity.java
package com.example.xinli;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.List;

public class BadgeWallActivity extends AppCompatActivity {

    private List<Badge> badgeList;
    private GridLayout badgeGrid;
    private LinearLayout badgeDetailLayout;
    private ScrollView mainScrollView;
    private ImageView detailBadgeImage;
    private TextView detailBadgeName;
    private TextView detailBadgeCondition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_badge_wall);
        
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initViews();
        initBadges();
        setupBadgeGrid();
        setupBackButton();
    }

    private void initViews() {
        badgeGrid = findViewById(R.id.badgeGrid);
        badgeDetailLayout = findViewById(R.id.badgeDetailLayout);
        mainScrollView = findViewById(R.id.mainScrollView);
        detailBadgeImage = findViewById(R.id.detailBadgeImage);
        detailBadgeName = findViewById(R.id.detailBadgeName);
        detailBadgeCondition = findViewById(R.id.detailBadgeCondition);
        
        // 默认隐藏详情布局
        badgeDetailLayout.setVisibility(View.GONE);
        
        // 点击详情布局返回网格视图
        badgeDetailLayout.setOnClickListener(v -> showBadgeGrid());
    }

    private void initBadges() {
        badgeList = new ArrayList<>();
        
        // 添加15个徽章示例数据
        badgeList.add(new Badge(R.drawable.picture, "新手启航", "开始心理健康之旅", "完成首次心理测试", true));
        badgeList.add(new Badge(R.drawable.picture, "坚持不懈", "连续打卡7天", "连续7天完成每日任务", true));
        badgeList.add(new Badge(R.drawable.picture, "知识达人", "学习10篇心理文章", "阅读10篇心理健康相关文章", false));
        badgeList.add(new Badge(R.drawable.picture, "情绪管理师", "完成情绪管理课程", "完成所有情绪管理训练", true));
        badgeList.add(new Badge(R.drawable.picture, "社交能手", "参与5次互动讨论", "在社区中参与5次讨论", false));
        badgeList.add(new Badge(R.drawable.picture, "冥想大师", "完成30次冥想练习", "累计完成30次冥想", true));
        badgeList.add(new Badge(R.drawable.picture, "助人为乐", "帮助3位用户", "在互助社区中帮助3位用户解决问题", false));
        badgeList.add(new Badge(R.drawable.picture, "健康生活", "连续21天健康打卡", "连续21天完成健康习惯打卡", true));
        badgeList.add(new Badge(R.drawable.picture, "心理专家", "完成所有心理课程", "完成平台所有心理健康课程", false));
        badgeList.add(new Badge(R.drawable.picture, "分享大使", "分享应用10次", "将应用分享给10位朋友", true));
        badgeList.add(new Badge(R.drawable.picture, "挑战者", "完成所有心理挑战", "完成平台所有心理挑战任务", false));
        badgeList.add(new Badge(R.drawable.picture, "感恩之心", "连续7天记录感恩日记", "连续7天完成感恩日记", true));
        badgeList.add(new Badge(R.drawable.picture, "睡眠专家", "改善睡眠质量", "连续14天记录良好睡眠", false));
        badgeList.add(new Badge(R.drawable.picture, "积极心态", "保持积极心态30天", "连续30天心态积极向上", true));
        badgeList.add(new Badge(R.drawable.picture, "心理卫士", "获得所有徽章", "收集平台所有徽章", false));
    }

    private void setupBadgeGrid() {
        badgeGrid.removeAllViews();
        
        int columnCount = 3; // 每行显示3个徽章
        int imageSize = getResources().getDimensionPixelSize(R.dimen.badge_image_size);
        
        for (int i = 0; i < badgeList.size(); i++) {
            Badge badge = badgeList.get(i);
            
            // 创建徽章布局
            LinearLayout badgeLayout = new LinearLayout(this);
            badgeLayout.setOrientation(LinearLayout.VERTICAL);
            badgeLayout.setPadding(16, 16, 16, 16);
            
            // 创建徽章图片
            ImageView badgeImage = new ImageView(this);
            badgeImage.setImageResource(badge.getImageResId());
            badgeImage.setLayoutParams(new LinearLayout.LayoutParams(imageSize, imageSize));
            badgeImage.setScaleType(ImageView.ScaleType.FIT_START);
            
            // 如果徽章未解锁，添加灰色遮罩效果
            if (!badge.isUnlocked()) {
                badgeImage.setAlpha(0.3f);
            }
            
            // 创建徽章名称
            TextView badgeName = new TextView(this);
            badgeName.setText(badge.getName());
            badgeName.setTextSize(12);
            badgeName.setGravity(android.view.Gravity.CENTER);
            badgeName.setMaxLines(2);
            badgeName.setEllipsize(android.text.TextUtils.TruncateAt.END);
            
            // 如果徽章未解锁，名称也变灰
            if (!badge.isUnlocked()) {
                badgeName.setAlpha(0.5f);
            }
            
            badgeLayout.addView(badgeImage);
            badgeLayout.addView(badgeName);
            
            // 设置点击事件
            final int index = i;
            badgeLayout.setOnClickListener(v -> showBadgeDetail(index));
            
            // 添加到网格布局
            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
            params.width = 0;
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
            params.setMargins(8, 8, 8, 8);
            badgeLayout.setLayoutParams(params);
            
            badgeGrid.addView(badgeLayout);
        }
    }

    private void showBadgeDetail(int badgeIndex) {
        Badge badge = badgeList.get(badgeIndex);
        
        // 设置详情信息
        detailBadgeImage.setImageResource(badge.getImageResId());
        detailBadgeName.setText(badge.getName());
        detailBadgeCondition.setText("获得条件: " + badge.getCondition());
        
        // 显示详情布局，隐藏网格布局
        badgeGrid.setVisibility(View.GONE);
        badgeDetailLayout.setVisibility(View.VISIBLE);
        mainScrollView.scrollTo(0, 0);
    }

    private void showBadgeGrid() {
        // 隐藏详情布局，显示网格布局
        badgeDetailLayout.setVisibility(View.GONE);
        badgeGrid.setVisibility(View.VISIBLE);
    }

    public void backIntervention(View view) {
        finish();
    }

    private void setupBackButton() {
        findViewById(R.id.backBtn1).setOnClickListener(this::backIntervention);
    }
}
